﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Diagnostics;
using System.Configuration;
using System.Collections;
using System.Web.Routing;
using Blogger.UI.Models;
using Blogger.UI.Data;
using POC.Configuration;
using POC.Azure.Storage;

namespace Blogger.UI.Controllers
{
    //[Initialization]
    public class BlogUIController : UIController
    {
        IBlogRepository Service = null;
        public BlogUIController():this(null)
        {
        }
        public BlogUIController(IBlogRepository repository)
        {
            InitApp(repository);
        }
        protected string logOnUrl;
        public void InitApp(IBlogRepository repository)
        {
            if (repository == null)
            {
                try
                {
                    // Need to place somewhere
                    AzureAccount account = new AzureAccount(true);
                    Service = new BlogRepository(account);
                }
                catch (Exception cx)
                {

                }
            }
            else
            {
                Service = repository;
            }
        }
        public ActionResult Logon()
        {
            return RedirectToAction("Login", "Home", new { });
        }
        public ActionResult Index()
        {           
            //if (IsServiceNotInitiated) return Logon();
            List<Blog> blogs = Service.GetBlogs();
            return View(blogs);
        }      
       
        //
        // GET: /BlogUI/Details/5

        public ActionResult Details(string id = null)
        {

            var blog = Service.GetBlog(id);
        
            if (blog == null)
            {
                return HttpNotFound();
            }
            return View(blog);
        }

        //
        // GET: /BlogUI/Create

        public ActionResult Create()
        {
            
            return View();
        }

        //
        // POST: /BlogUI/Create

        [HttpPost]
        public ActionResult Create(Blog blog)
        {          
            try
            {
                if (ModelState.IsValid)
                {
                    blog.ID = GetNewID();
                    Service.CreateBlog(blog);
                    return RedirectToAction("Index");
                }
            }
            catch (Exception ecx)
            {
                ModelState.AddModelError("EX", ecx);
            }
            return View(blog);
        }

        //
        // GET: /BlogUI/Edit/5

        public ActionResult Edit(string id = null)
        {
            var blog = Service.GetBlog(id);
            if (blog == null)
            {
                return HttpNotFound();
            }
            return View(blog);
        }

        //
        // POST: /BlogUI/Edit/5

        [HttpPost]
        public ActionResult Edit(Blog blog)
        {

            if (ModelState.IsValid)
            {
                var blogServer = Service.GetBlog(blog.ID);
                if (blogServer == null) return View(blog);
                Service.EditBlog(blog);
                return RedirectToAction("Index");
            }
            return View(blog);
        }

        //
        // GET: /BlogUI/Delete/5

        public ActionResult Delete(string id = null)
        {

            var blog = Service.GetBlog(id);
            if (blog == null)
            {
                return HttpNotFound();
            }
            return View(blog);
        }

        //
        // POST: /BlogUI/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {

            var blog = Service.GetBlog(id);
            Service.DeleteBlog(blog);
            return RedirectToAction("Index");
        }
        public ActionResult Posting(string id)
        {            
            Blog blog = Service.FindBlog(id);
            List<Posting> posts = Service.GetPostingsByBlogID(id);
            ViewBag.Blog = blog;
            ViewBag.BlogID = id;
            return View(posts);
        }
        /// <summary>
        /// ////////////////////////////////////////////////////////
        /// </summary>
        /// <param name="id"></param>
        /// <param name="blogId"></param>
        /// <returns></returns>
        public ActionResult CreatePosting(string id, string blogId)
        {            
            Posting post = new Posting();
            post.ID =  GetNewID();
            post.BlogID = blogId;
            ViewBag.BlogID = id;
            return View(post);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreatePosting(Posting posting)
        {
            
            ViewBag.BlogID = posting.BlogID;
            try
            {
                if (ModelState.IsValid)
                {
                    posting.ID = string.IsNullOrEmpty(posting.ID) ? posting.ID: GetNewID(); 
                    Service.CreatePosting(posting.BlogID, posting);
                    return RedirectToAction("Posting", new { id = posting.BlogID });
                }
            }
            catch (Exception ecx)
            {
                ModelState.AddModelError("EX", ecx);
            }
            return View(posting);
        }

        public ActionResult PreviewPosting(string id)
        {

            Posting posting = Service.GetPosting(id);
            return View(posting);
        }
        public ActionResult PublishPosting(string id)
        {

            Posting posting = Service.GetPosting(id);
            return View(posting);
        }
        [HttpPost, ActionName("PublishPosting")]
        public ActionResult PublishPostingConfirmed(string id)
        {

            Posting posting = Service.GetPosting(id);
            Service.PublishPosting(posting);
            return RedirectToAction("Posting", new { id = posting.BlogID });
        }

        public ActionResult EditPosting(string id)
        {

            Posting posting = Service.GetPosting(id);
            return View(posting);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditPosting(Posting posting)
        {
            
            ViewBag.BlogID = posting.BlogID;
            try
            {
                if (ModelState.IsValid)
                {
                    Posting postingServer = Service.GetPosting(posting.ID);
                    //Exchange
                    if (postingServer == null)
                        return View(posting);
                    Service.EditPosting(posting);
                    return RedirectToAction("Posting", new { id = posting.BlogID });
                }
            }
            catch (Exception ecx)
            {
                ModelState.AddModelError("EX", ecx);
            }
            return View(posting);
        }

        public ActionResult DeletePosting(string id)
        {

            Posting posting = Service.GetPosting(id);
            return View(posting);
        }
        [HttpPost, ActionName("DeletePosting")]
        public ActionResult DeletePostingConfirmed(string id)
        {

            Posting posting = Service.GetPosting(id);
            Service.DeletePosting(posting);
            return RedirectToAction("Posting", new { id = posting.BlogID });
        }       
        //
        // GET: /BlogUI/
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}